package com.gubei.oss.adapter;

import com.gubei.oss.entity.StorageTypeEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class StorageAdapterFactory implements InitializingBean {

    @Resource
    private List<StorageAdapter> storageAdapterList;

    private Map<StorageTypeEnum, StorageAdapter> storageAdapterMap = new HashMap<>();

    public StorageAdapter getStorageAdapter(String type) {
        StorageTypeEnum storageTypeEnum = StorageTypeEnum.getByType(type);
        return storageAdapterMap.get(storageTypeEnum);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        for (StorageAdapter storageAdapter : storageAdapterList) {
            storageAdapterMap.put(storageAdapter.getStorageType(), storageAdapter);
        }
    }


}
